package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/7.
 */

public class MaxImagerViewPager extends ViewPager {

    private boolean isstart = false;
    private long time = 2000;
    private List<View> list = new ArrayList<>();
    private Timetask timetask;

    public MaxImagerViewPager(Context context) {
        this(context, null);
    }

    public MaxImagerViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        timetask = new Timetask(this);
    }

    /**
     * 按下停止轮播， 如果离开的时间 >= 2000毫秒,那么ViewPager继续轮播
     */
    public void startlop() {
        if (!isstart) {
            isstart = true;
            postDelayed(timetask, time);
        }

    }

    public void stoplop() {
        isstart = false;
        removeCallbacks(timetask);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stoplop();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        startlop();
    }

    public static class Timetask implements Runnable {
        private WeakReference<MaxImagerViewPager> weakReference;

        public Timetask(MaxImagerViewPager maxImagerViewPager) {
            weakReference = new WeakReference<MaxImagerViewPager>(maxImagerViewPager);
        }

        @Override
        public void run() {
            MaxImagerViewPager maxImagerViewPager = weakReference.get();
            maxImagerViewPager.setCurrentItem(maxImagerViewPager.getCurrentItem() + 1
            );
            maxImagerViewPager.postDelayed(maxImagerViewPager.timetask, maxImagerViewPager.time);
        }
    }

    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (VISIBLE == visibility) {
            startlop();
        } else if (INVISIBLE == visibility) {
            stoplop();
        }
    }

    @Override

    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {

            case MotionEvent.ACTION_DOWN:

                //按下停止
                stoplop();
                break;

            case MotionEvent.ACTION_UP:
                startlop();


                break;

        }

        return super.onTouchEvent(ev);

    }

    private boolean timetag = false;

    //监测两次时间的间隔


    //提供方法设置viewpager的适配器
    public void setMaXadapter(List<View> list, Context context) {
        this.list = list;
        setAdapter(new Myadapter(context));
        setCurrentItem(200);

    }

    public class Myadapter extends PagerAdapter {
        private Context context;


        public Myadapter(Context context) {
            this.context = context;
        }

        @Override

        public int getCount() {

            return Integer.MAX_VALUE;

        }

        @Override

        public boolean isViewFromObject(View view, Object object) {

            return view == object;

        }

        @Override

        public Object instantiateItem(ViewGroup container, final int position) {

            final int i = position % list.size();
            View view = list.get(i);
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            if (container.equals(view.getParent())) {
                container.removeView(view);
            }
            Log.d("TAG", "instantiateItem: ");
            container.addView(view);
            return view;
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            container.removeView(list.get(position % list.size()));
        }

//        @Override
//        public void destroyItem(View container, int position, Object object) {
////            super.destroyItem(container, position, object);
//        }
    }

}
